package kr.hkit.androidcomplete02.XMLparser;

import java.util.ArrayList;

import kr.hkit.androidcomplete02.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter{

	Context context;
	ArrayList<Item> itemLists;
	LayoutInflater inflater;
	
	public MyAdapter(Context context, ArrayList<Item> aLists) {
		super();
		this.itemLists = aLists;
		inflater = LayoutInflater.from(context);
	} //컨텍스트 정보 받아와야해서 생성자만듬

	@Override
	public int getCount() {
		return itemLists.size();
	}

	@Override
	public Object getItem(int position) {
		return itemLists.get(position).getItemName();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder{ //속도 빠르게 하기 위해서 뷰홀더씀
		
		TextView itemName;
		TextView makerName;
		TextView itemPrice;
		}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View itemLayout = convertView;
		ViewHolder viewHolder = null;
		
		if (itemLayout == null) {//처음 생성될때 
			itemLayout = inflater.inflate(R.layout.itemrow, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.itemName = (TextView) itemLayout.findViewById(R.id.itemName);
			viewHolder.makerName = (TextView) itemLayout.findViewById(R.id.makerName);
			viewHolder.itemPrice = (TextView) itemLayout.findViewById(R.id.itemPrice);
			itemLayout.setTag(viewHolder);
		} else { //재활용될때
			viewHolder = (ViewHolder) itemLayout.getTag();
		}//매핑
		viewHolder.itemName.setText(itemLists.get(position).getItemName());
		viewHolder.makerName.setText(itemLists.get(position).getMakerName());
		viewHolder.itemPrice.setText(itemLists.get(position).getItemPrice() + "");
		return itemLayout;
	}

}
